using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Expression;
using NHibernate.SqlCommand;


namespace FutureOpen.Core.func.db
{
    public class MyTagUsr : MyDBEntity
    {
        private Usr usr;
        private IList<Tagusr> mytags;

        public MyTagUsr(int uid)
        {
            this.usr = new Usr();
            this.usr.Usr_id = uid;
        }
        public MyTagUsr(Usr usr)
        {
            this.usr = usr; 
        }        
        private void LoadMyTag()
        {
            using (ISession session = base.factory.OpenSession())
            {
                try
                {                    
                    IList<Tagusrmap> tags = session.CreateCriteria(typeof(Tagusrmap)).Add(Expression.Eq("Tagusrmap_usr", usr)).List<Tagusrmap>();
                    mytags = new List<Tagusr>();
                    foreach (Tagusrmap map in tags)
                    {
                        mytags.Add(map.Tagusrmap_tag);
                    }                                     
                }
                catch(Exception)
                {
                    this.mytags = null;
                }
            }
        }

        public void AddTag(string text)
        {
            text = text.ToLower();
            using (ISession session = base.factory.OpenSession())
            {
                Tagusr tag = null;
                IList<Tagusr> tags = session.CreateCriteria(typeof(Tagusr))
                                                   .Add(Expression.Eq("Tagusr_text", text))
                                                   .List<Tagusr>();

                ITransaction transation = session.BeginTransaction();
                if (tags.Count == 0)
                {
                    tag = new Tagusr();
                    tag.Tagusr_text = text;
                    tag.Tagusr_count = 1;

                    session.Save(tag);
                    session.Flush();
                }
                else
                {
                    tag = tags[0];
                    tag.Tagusr_count++;
                    session.Update(tag);
                }

                //insert record        
                Tagusrmap map = new Tagusrmap(tag, usr);
                session.Save(map);
                transation.Commit();
            } 
 
        }
        public void AddTag(Tagusr tag)
        {
            tag.Tagusr_text = tag.Tagusr_text.ToLower();
            using (ISession session = base.factory.OpenSession())
            {
                ITransaction transation = session.BeginTransaction();
                tag.Tagusr_count++;
                session.Update(tag);            

                //insert record
                Tagusrmap map = new Tagusrmap(tag, usr);
                session.Save(map);
                transation.Commit();
                
            } 
        }

        public Tagusr LoadTagByText(string text)
        {
            using (ISession session = base.factory.OpenSession())
            {
                IList<Tagusr> tags = session.CreateCriteria(typeof(Tagusr))
                                                   .Add(Expression.Eq("Tagusr_text", text))
                                                   .List<Tagusr>();
                if (tags.Count == 0)
                {
                    return null;
                }
                else
                {
                    return tags[0];
                }
            }
        }
        
        public IList<Tagusr> GetUsrTags()
        {
            if (mytags == null)
            {
                LoadMyTag();
            }
            return this.mytags;
        }
    }
}
